package hot;

import java.util.LinkedList;
import java.util.Queue;

public class Num437 {
    int count = 0;
    long sum = 0;
    public int pathSum(TreeNode root, int targetSum) {
        if(root == null) return 0;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty()) {
            TreeNode node = queue.poll();
            dfs(node,targetSum);
            if(node.left != null) queue.add(node.left);
            if(node.right != null) queue.add(node.right);
        }
        return count;
    }
    public void dfs(TreeNode root,int targetSum) {
        if(root == null) return;
        sum += root.val;
        if(sum == targetSum) {
            count++;
        }
        dfs(root.left,targetSum);
        dfs(root.right,targetSum);
        sum -= root.val;
    }
}
